package hard;

import com.eclipsesource.json.Json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 726. 原子的数量
 */
public class CountOfAtoms {

    public static String stringToString(String input) {
        if (input == null) {
            return "null";
        }
        return input.toString();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            String formula = stringToString(line);

            String ret = new Solution().countOfAtoms(formula);

            String out = (ret);

            System.out.print(out);
        }
    }

    static class Solution {
        public String countOfAtoms(String formula) {
            Map<String, Integer> map = new TreeMap<>();
            dfs(formula, map, 1);
            StringBuilder sb = new StringBuilder();
            for (String str : map.keySet()) {
                sb.append(str);
                if (map.get(str) > 1) {
                    sb.append(map.get(str));
                }
            }
            return sb.toString();
        }

        void dfs(String formula, Map<String, Integer> map, int multiple) {
            if (formula.contains("(")) {
                int startIndex = formula.indexOf("(") + 1;
                int endIndex = getEnd(formula,startIndex);
                if (formula.lastIndexOf(")") == formula.length() - 1 || !Character.isDigit(formula.charAt(endIndex + 1))) {
                    dfs(formula.substring(startIndex, endIndex), map, 1);
                    formula = formula.subSequence(0, startIndex - 1) + formula.substring(endIndex + 1);
                } else {
                    String numStr = "1";
                    for (int i=endIndex+1;i<formula.length();i++){
                        if (!Character.isDigit(formula.charAt(i))){
                            numStr = formula.substring(endIndex+1,i);
                            break;
                        }else if (i==formula.length()-1){
                            numStr = formula.substring(endIndex+1,i+1);
                        }
                    }
                    dfs(formula.substring(startIndex, endIndex), map, Integer.parseInt(numStr) * multiple);
                    formula = formula.subSequence(0, startIndex - 1) + formula.substring(endIndex + numStr.length()+1);
                }
            }
            if (formula.contains("(")) {
                dfs(formula,map,multiple);
                return;
            }
            int startIndex = 0;
            StringBuilder numSB = new StringBuilder();
            for (int i = 0; i <= formula.length(); i++) {
                if (i < formula.length() && Character.isUpperCase(formula.charAt(i)) || i == formula.length()) {
                    String key;
                    if (numSB.toString().equals("")) {
                        key = formula.substring(startIndex, i);
                    } else {
                        key = formula.substring(startIndex, i - numSB.length());
                    }
                    if (!key.equals("")) {
                        if (numSB.toString().equals("")) {
                            map.put(key, map.getOrDefault(key, 0) + 1 * multiple);
                        } else {
                            map.put(key, map.getOrDefault(key, 0) + Integer.parseInt(numSB.toString()) * multiple);
                        }
                    }
                    startIndex = i;
                    numSB = new StringBuilder();
                }
                if (i < formula.length()) {
                    if (Character.isDigit(formula.charAt(i))) {
                        numSB.append(formula.charAt(i));
                    }
                }

            }
        }

        int getEnd(String formula,int start){
            int count = 0;
            for (int i=start;i<formula.length();i++){
                if ('('==formula.charAt(i)){
                    count++;
                }
                if (')'==formula.charAt(i)){
                    if (count==0){
                        return i;
                    }
                    count--;
                }
            }
            return -1;
        }
    }
}
